﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace YogaStudio.WebUI.Helper
{
    public class Encription
    {
        private static string _privateKey = "<RSAKeyValue><Modulus>nWKqXgTBiVjpzW16z+2ibwxXzMajWmU0+I6INwU4PCPTVZoNwpvk7/eBvT5NGgBHFVxVwS41TofibslA7A3W5ZmJldDQPQrFKU3CvrGNSKH6vRgtM/XyOYJYhTPTc3XaCVbezT5/2zWT+rRH3PNQDlN0EpSDIwIa1xgfqFDhtms=</Modulus><Exponent>AQAB</Exponent><P>1OWz/hNUqV+5+8VUYVxmU4PfocMlYp7M1eB4Ut0WOXslEhWJIG8mOMp07nGUN9WRbRo/hB+Gg6nzIbztgCDzEQ==</P><Q>vT/Tk680SUbc92j6KxllZhWXIChpAhKqZHnG/GR9N8ZgcmEvWjVK7s/hs2hGSRyArYrFYX+UsQHhdRK2mNuZuw==</Q><DP>hq7ac33fLVd13SU1AHLn+SWQMFG4MO+tPhDHjgR3rt2M0if1nCM01vCdkMshrUDaw167l+YpvtmhClLvHQO7QQ==</DP><DQ>Hk5dVHYnOmDVu+0cutArU8i7VdQC1OkmTkbOHdb90nO7ODXl/l0wbcPukett7gFZffEOIMnshtgVQclIYnwwRw==</DQ><InverseQ>eJh9pWxptQij1AJUwTMAq4TUEC01/yNp9XPW8LLOnLMlJQ8m38+806JLwqdNAMCZi2dw4ydwRu4cSbdjs8SvCg==</InverseQ><D>AQ2KyUpFENb8nPGmv+TDLdVb0IVMcuyhP/fPOjdItLqA1q8wOeMEuD8sA6zZCuJrB5qNvo9DpiwFrG1c2K+SdHFyh9NFmhwVBeP7GckaytLeLfm0PjMWjO0z86D6ewexVQVYVQPK6USis5iqNoPWTacw51la95ovaMKnMrhyjWE=</D></RSAKeyValue>";
        private static string _publicKey = "<RSAKeyValue><Modulus>nWKqXgTBiVjpzW16z+2ibwxXzMajWmU0+I6INwU4PCPTVZoNwpvk7/eBvT5NGgBHFVxVwS41TofibslA7A3W5ZmJldDQPQrFKU3CvrGNSKH6vRgtM/XyOYJYhTPTc3XaCVbezT5/2zWT+rRH3PNQDlN0EpSDIwIa1xgfqFDhtms=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

        private static UnicodeEncoding _encoder = new UnicodeEncoding();

        public static string Decrypt(string data)
        {
            var rsa = new RSACryptoServiceProvider();
            try
            {
                byte[] dataByte = Convert.FromBase64String(data);


                rsa.FromXmlString(_privateKey);
                var decryptedByte = rsa.Decrypt(dataByte, false);
                return _encoder.GetString(decryptedByte);
            }
            catch
            {
                return null;
            }
        }

        public static string Encrypt(string data)
        {
            var rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(_publicKey);
            var dataToEncrypt = _encoder.GetBytes(data);
            var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
            return Convert.ToBase64String(encryptedByteArray);
        }
    }
}